<template>
  <el-dialog title="分配权限" :visible="showPermission" @close="closeEvent">
    <!-- default-checked-keys:只管选中，至于其它项是否选中它不关心 -->
    <el-tree ref="tree" check-strictly :data="list" show-checkbox :props="{label:'name'}" node-key="id" :default-checked-keys="permIds" />

    <template #footer>
      <div style="text-align: center;">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>

  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleById, sysRoleAssignPrem } from '@/api/setting'
export default {
  props: {
    showPermission: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      list: [],
      permIds: [],
      id: ''
    }
  },
  methods: {
    closeEvent() {
      this.$emit('update:showPermission', false)
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取权限点
    async getData() {
      if (this.list.length === 0) {
        const res = await sysPermission()
        this.list = changeData(res.data, '0')
        console.log('权限点：', res)
      }
    },
    // 获取角色详情
    async getInfo(id) {
      this.id = id
      const res = await sysRoleById(id)
      this.permIds = res.data.permIds
      // this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log(res)
    },
    // 确定点击
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('设置权限成功')
      this.closeEvent()
    }
  }
}
</script>
<style>
</style>
